
package com.greenant.store.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.greenant.store.R;
import com.greenant.store.bean.Order;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.Const;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lsarah.utils.SystemBarTintManager;

public class EmrecvDetailActivity extends BaseActivity {
    private final String TAG = "EmrecvDetailActivity";

    private View mBackView;

    private Context context;
    private PullToRefreshScrollView scrollView;

    private RelativeLayout nodataView;
    private LinearLayout dataView;

    private TextView emrecvid_desc;
    private TextView emrecv_status_desc;
    private TextView emrecv_singer_desc;
    private TextView emrecv_createdAt_desc;
    private TextView emrecv_SingedTime_desc;
    private TextView emrecv_EMCompany_desc;
    private TextView emrecv_senderName_desc;
    private TextView emrecv_senderPhone_desc;
    private TextView emrecv_senderAddr_desc;
    private TextView emrecv_receiverName_desc;
    private TextView emrecv_receiverPhone_desc;
    private TextView emrecv_receiverAddr_desc;

    private Handler mHandler = new Handler(Looper.getMainLooper());

    private OnClickListener mOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.equals(mBackView)) {
                EmrecvDetailActivity.this.finish();
            }
        }
    };

    private RetrieveDataListener mRetrieveDataListener = new RetrieveDataListener() {
        @Override
        public void dataRetrieved(int requestId, Object data) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemBarTintManager.setContentView(this, R.layout.activity_emrecv_detail,
                getResources().getColor(R.color.firebrick));
        context = this;
        mBackView = findViewById(R.id.order_create_top_arrow_left);
        scrollView = (PullToRefreshScrollView) findViewById(R.id.scrollView);
        scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                refreshOrderDetail();
            }
        });

        nodataView = (RelativeLayout) findViewById(R.id.nodataView);
        dataView = (LinearLayout) findViewById(R.id.dataView);

        emrecvid_desc = (TextView) findViewById(R.id.emrecvid_desc);
        emrecv_status_desc = (TextView) findViewById(R.id.emrecv_status_desc);
        emrecv_singer_desc = (TextView) findViewById(R.id.emrecv_singer_desc);
        emrecv_createdAt_desc = (TextView) findViewById(R.id.emrecv_createdAt_desc);
        emrecv_SingedTime_desc = (TextView) findViewById(R.id.emrecv_SingedTime_desc);
        emrecv_EMCompany_desc = (TextView) findViewById(R.id.emrecv_EMCompany_desc);
        emrecv_senderName_desc = (TextView) findViewById(R.id.emrecv_senderName_desc);
        emrecv_senderPhone_desc = (TextView) findViewById(R.id.emrecv_senderPhone_desc);
        emrecv_senderAddr_desc = (TextView) findViewById(R.id.emrecv_senderAddr_desc);
        emrecv_receiverName_desc = (TextView) findViewById(R.id.emrecv_receiverName_desc);
        emrecv_receiverPhone_desc = (TextView) findViewById(R.id.emrecv_receiverPhone_desc);
        emrecv_receiverAddr_desc = (TextView) findViewById(R.id.emrecv_receiverAddr_desc);

        mBackView.setOnClickListener(mOnClickListener);

        initData();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void refreshOrderDetail() {

        dataView.setVisibility(View.INVISIBLE);
        nodataView.setVisibility(View.VISIBLE);
        if (Const.order != null) {

            RetrieveDataAsyn.getInstance().getOrderDetail(Const.order.getObjectId(), 0,
                    new RetrieveDataListener() {

                        @Override
                        public void dataRetrieved(int requestId, Object data) {

                            if (data != null) {
                                Order tempOrder = (Order) data;

                                Const.order.put(Order.ATTR_ORDERITEMS,
                                        tempOrder.getString(Order.ATTR_ORDERITEMS));

                                Const.order.setStatusCode(tempOrder.getStatusCode());
                                Const.order.setAddress(tempOrder.getAddress());
                                Const.order.setOrderType(tempOrder.getOrderType());
                                Const.order.setPayWay(tempOrder.getPayWay());
                                Const.order.setDeliveryTime(tempOrder.getDeliveryTime());
                                Const.order.setDeliveryWay(tempOrder.getDeliveryWay());
                                Const.order.setPayTime(tempOrder.getPayTime());
                                Const.order.setTotalCost(tempOrder.getTotalCost());
                                Const.order.setDeliveryCost(tempOrder.getDeliveryCost());
                                initData();

                            }
                            dataView.setVisibility(View.VISIBLE);
                            nodataView.setVisibility(View.INVISIBLE);
                            scrollView.onRefreshComplete();
                        }
                    });

        } else {
            dataView.setVisibility(View.VISIBLE);
            nodataView.setVisibility(View.INVISIBLE);
        }

    }

    private void initData() {
        if (Const.emrecv == null) {
            return;
        }
        String[] emrecvcode = context.getResources().getStringArray(R.array.emrecv_status_code);

        emrecvid_desc.setText(Const.emrecv.getEMId());
        
        try {
			emrecv_status_desc.setText(emrecvcode[Const.emrecv.getStatus()]);
		} catch (Exception e) {
			e.printStackTrace();
		}

        emrecv_singer_desc.setText(Const.emrecv.getSinger());
        emrecv_createdAt_desc
                .setText(DateFormat.format("yyyy-MM-dd HH:mm:ss", Const.emrecv.getCreatedAt()));
        if(Const.emrecv.getSingedTime()!=null)
        {
        	emrecv_SingedTime_desc
            .setText(DateFormat.format("yyyy-MM-dd HH:mm:ss", Const.emrecv.getSingedTime()));
        }
        if(Const.emrecv.getEMCompany()!=null)
        {
            emrecv_EMCompany_desc.setText(Const.emrecv.getEMCompany().getCompanyName());    
        }
        emrecv_senderName_desc.setText(Const.emrecv.getSenderName());
        emrecv_senderPhone_desc.setText(Const.emrecv.getSenderPhone());
        emrecv_senderAddr_desc.setText(Const.emrecv.getSenderAddr());
        emrecv_receiverName_desc.setText(Const.emrecv.getReceiverName());
        emrecv_receiverPhone_desc.setText(Const.emrecv.getReceiverPhone());
        emrecv_receiverAddr_desc.setText(Const.emrecv.getReceiverAddr());

    }

}
